﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Mozlite.Cryptography
{
    internal class DecryptTransformer
    {
        internal DecryptTransformer(EncryptionAlgorithm algorithm)
        {
            this.algorithm = algorithm;
        }

        private EncryptionAlgorithm algorithm;
        private byte[] iv;

        internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey)
        {
            switch (algorithm)
            {
                case EncryptionAlgorithm.Des:
                    {
                        DES des = new DESCryptoServiceProvider();
                        des.Mode = CipherMode.CBC;
                        des.Key = bytesKey;
                        des.IV = iv;
                        return des.CreateDecryptor();
                    }
                case EncryptionAlgorithm.TripleDes:
                    {
                        TripleDES des3 = new TripleDESCryptoServiceProvider();
                        des3.Mode = CipherMode.CBC;
                        return des3.CreateDecryptor(bytesKey, iv);
                    }
                case EncryptionAlgorithm.Rc2:
                    {
                        RC2 rc2 = new RC2CryptoServiceProvider();
                        rc2.Mode = CipherMode.CBC;
                        return rc2.CreateDecryptor(bytesKey, iv);
                    }
                case EncryptionAlgorithm.Rijndael:
                    {
                        Rijndael rijndael = new RijndaelManaged();
                        rijndael.Mode = CipherMode.CBC;
                        return rijndael.CreateDecryptor(bytesKey, iv);
                    }
                default:
                    {
                        throw new CryptographicException("不支持“" + algorithm + "”算法。");
                    }
            }
        }

        internal byte[] IV
        {
            set { iv = value; }
        }
    }
}
